package leetcode.problems;

import java.util.Stack;

/**
 * _0410 Min Stack
 * 移除元素
 * Created by gmwang on 2018/3/23
 */
public class _0410MinStack {
    /**
     * Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
         push(x) -- Push element x onto stack.
         pop() -- Removes the element on top of the stack.
         top() -- Get the top element.
         getMin() -- Retrieve the minimum element in the stack.
         Example:
             MinStack minStack = new MinStack();
             minStack.push(-2);
             minStack.push(0);
             minStack.push(-3);
             minStack.getMin();   --> Returns -3.
             minStack.pop();
             minStack.top();      --> Returns 0.
             minStack.getMin();   --> Returns -2.
                 设计一个堆栈，支持推送、弹出、顶，并在常量时间内检索最小元素。
                 推（x）-将元素x推入堆栈。
                 pop() --删除栈顶元素。
                 top() --获得顶级元素。
                 getmin()检索堆栈中的最小元素。
     */
    /**
     * 思路：
     * 1. 查看stack的api就好了。
     *
     * @param
     * @return
     */

    public static void main(String[] args) {
        MinStack obj = new MinStack();
        obj.push(1);
        obj.push(2);
        obj.push('a');
        obj.push('b');
        obj.pop();
        int param_3 = obj.top();
        int param_4 = obj.getMin();
        System.out.println(param_3);
        System.out.println(param_4);
    }
}

class MinStack {
    private Stack stack;

    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack();
    }

    public void push(int x) {
        stack.push(x);
    }

    public void pop() {
        stack.pop();
    }

    public int top() {
        return (int)stack.peek();
    }

    public int getMin() {
        int min = (int)stack.get(0);
        for(int i = 0;i<stack.size();i ++){
            if(min > (int)stack.get(i)) min = (int)stack.get(i);
        }
        return min;
    }
}
